package com.hc.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.tagext.TryCatchFinally;

import net.sf.json.JSONObject;

import com.hc.bean.BSizeBean;
import com.hc.bean.RoleBean;
import com.hc.comm.CommData;
import com.hc.comm.Service.BSizeServie;
import com.hc.comm.Service.GetRoleService;


public class RoleServlet extends HttpServlet{
		@Override
		protected void doPost(HttpServletRequest req, HttpServletResponse resp)
				throws ServletException, IOException {

			String types  = req.getParameter("types");
			
			if("inputRoleNumber".equals(types)){
				
				getRole(req,resp);
				
			}else if("setBSize".equals(types)){
				
				setBSize(req,resp);
			}
	
		}
		
		
		private void setBSize(HttpServletRequest req, HttpServletResponse resp) {

			String bSizeNumberStr  = req.getParameter("bSizeNumber").trim();
			HttpSession session = req.getSession();  
			RoleBean bean = (RoleBean) session.getAttribute("roleBean");
			
			String msg = "";
			
			try {
				int  bSizeNumber = Integer.parseInt(bSizeNumberStr);
				if(bean!=null){
					
					bean.getOrder().setBroadbandSize(bSizeNumber+"");
					msg = "true";
				}else{
					
					msg="没有找到你的登陆信息";
				}
			} catch (Exception e) {
				msg="请输入数字，不要输入其他字符";
			}
			
			returnMsg(req,resp,msg);
		}


		private void getRole(HttpServletRequest req, HttpServletResponse resp) {
	
			
			String roleNumber  = req.getParameter("roleNumber");
			
			RoleBean bean=	GetRoleService.getRole(roleNumber);
//			RoleBean bean=			CommData.roles.get(roleNumber);
//			
			
			HttpSession session = req.getSession();  
			session.setAttribute("roleBean", bean);
			
			String msg="false";
			if(bean!=null){
				JSONObject json = JSONObject.fromObject(bean);
				msg = json.toString();
			}

			returnMsg(req,resp,msg);
			
		}

		private void returnMsg(HttpServletRequest req, HttpServletResponse resp,String msg) {
			
			resp.setCharacterEncoding("utf-8");
//			resp.setHeader("Content-type", "text/html;charset=UTF-8");
			PrintWriter w;
			try {
				w = resp.getWriter();
				w.write(msg);
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}

		@Override
		protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
			doPost(req, resp);
		}
}
